function random(min, max) {
  if (arguments.length === 2) {
    return Math.floor(min + Math.random() * ((max + 1) - min))
  } else {
    return null;
  }
}

/**
 * shareTrigger
 * 事件触发分享
 * dateinfo.js中分享事件触发 onMessage
 */
 export default function({today, tapDay, userInfo}){
  let   randomDiff = 0
  let   randomDay = null
  let   isSameDay = false
  let   messageHint = ''
  let   messageBody = ''
  let   messageDate = ''
  let   oriMessageHint = ''
  let   oriMessageBody = ''
  if (tapDay.timestamp === today.timestamp) {
    isSameDay = true
    const randomDays = [3, 5, 2, 4, 1]
    const index = random(0, (randomDays.length-1))
    randomDiff = randomDays[index]
    randomDay = tapDay.offset(randomDiff)
    tapDay = randomDay
  }
  if (tapDay.timestamp < today.timestamp) {
    randomDiff = randomDays[index]
    randomDay = tapDay.offset(randomDiff)
  }
  messageDate = tapDay
  const targetDate = isSameDay ? `${randomDay.month+'/'+randomDay.day}日` : tapDay.month+'/'+tapDay.day+'日'
  wx.modalx.showModal({
    editable: {
      $$id: 'message-share',
      itemClass: 'message-share-edit',
      ready(){
        messageHint = ''
        messageBody = ''
      },
      "@input": {
        placeholder: '提示信息，猜中留言的线索',
        itemStyle: 'margin-bottom: 0.5em;',
        bindinput: 'onShareInput',
        maxlength: 20,
        value: wx.modalShare.oriMessageHint||''
      },
      '@textarea': {
        placeholder: `请填写信息`,
        maxcount: 50, 
        maxlength: 50,
        bindinput: 'onShareTextarea',
        value: wx.modalShare.oriMessageBody||'',
        strCount: (wx.modalShare.oriMessageBody&&wx.modalShare.oriMessageBody.length) || 0
      },
      onShareInput(e){
        messageHint = e.detail.value
        oriMessageHint = messageHint
      },
      onShareTextarea(e, param, inst){
        const value = e.detail.value;
        messageBody = value
        oriMessageBody = messageBody
        this.update({
          '@input.value': oriMessageHint,
          '@textarea.value': oriMessageBody,
          '@textarea.strCount': value.length
        })
      },
    },
    mask: false,
    height: 46,
    width: 85,
    confirmText: '分享',
    title: [
      {title: '@分享'}, 
      {title: `好友将在${isSameDay ? '《随机: '+targetDate+` ${randomDiff}天后》` : `《${targetDate}》`}解锁下列信息。${isSameDay ? `或在日历中指定解锁日期` : ''}`},
      // {title: `将信息@给好友, 好友于${targetDate}${isSameDay ? `${randomDiff}天后可见（随机指定），或在日历中指定一个未来的日期` : '可见'}`},
    ],
    success (res) {
      if (res.confirm) {
        // messageHint
        // messageBody
        if (tapDay.timestamp <= today.timestamp) {
          wx.showToast({title: '选择大于今天的日期并分享', icon: 'none'})
          return 'KEEP'
        } 
        
        if (messageBody.length < 5) {
          wx.showToast({title: '内容字数不小于5', icon: 'none'})
          return 'KEEP'
        } else {
          if (userInfo && userInfo.nickName) {
            messageHint = '有一份来自'+userInfo.nickName+'的留言' + (messageHint ? ':'+messageHint : '')
          } else {
            messageHint = '有一份给您的留言' + (messageHint ? ':'+messageHint : '')
          }
          // messageHint = messageHint+`(${targetDate}可查看)`

          wx.modalShare.oriMessageHint = oriMessageHint
          wx.modalShare.oriMessageBody = oriMessageBody
          wx.modalShare.messageHint = messageHint
          wx.modalShare.messageBody = messageBody
          wx.modalShare.messageDate = messageDate
          wx.modalShare.userInfo = userInfo

          wx.modalShare.showModal({
            content: messageHint+`(${targetDate}可查看)`,
            showCancel: false,
            itemClass: 'modal-message-share',
            height: 13,
            width: 80,
            mask: false,
            confirmButton: {
              itemStyle: 'grid-column: 1 / 3; width: 100%;',
              "@button": {
                "open-type": 'share',
                plain: true,
                title: '分享',
                itemStyle: 'width: 100%; height: 40px;line-height: 40px;',
                tap: 'onConfirm'
              }
            },
            
            success(resp){
              resp.close()
            }
          })
        }
      }
    }
  })
}